package com.sky.controller.user;


import com.sky.dao.SetDefaultDTO;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "用户地址簿")
@Slf4j
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @ApiOperation("查询当前登录用户的所有地址信息")
    @GetMapping("/list")
    public Result<List<AddressBook>> list() {
        List<AddressBook> list = addressBookService.list();
        return Result.success(list);
    }

    @ApiOperation("查詢默認地址")
    @GetMapping("/default")
    public Result<AddressBook> defaultAddress() {
        AddressBook addressBook = addressBookService.defaultAddress();
        return Result.success(addressBook);
    }

    @ApiOperation("根據id查詢地址")
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable("id") Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    @ApiOperation("根據id修改地址")
    @PutMapping()
    public Result update(@RequestBody AddressBook addressBook) {
        addressBookService.update(addressBook);
        return Result.success();
    }

    @ApiOperation("新增地址")
    @PostMapping()
    public Result save(@RequestBody AddressBook addressBook) {
        addressBookService.save(addressBook);
        return Result.success();
    }

    @ApiOperation("設置默認地址")
    @PutMapping("/default")
    public Result setDefault(@RequestBody SetDefaultDTO setDefaultDTO) {
        addressBookService.setDefault(setDefaultDTO);
        return Result.success();
    }

    @ApiOperation("根據id刪除地址")
    @DeleteMapping()
    public Result delete(@RequestParam("id") Long id) {
        addressBookService.delete(id);
        return Result.success();
    }
}
